package com.contorller.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.entity.User;
import com.exception.NoLoginException;


public class LoginInterceptor  extends HandlerInterceptorAdapter  {
	/**
	 * 请求之前拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("拦截url : "+request.getServletPath());
		
		//过滤不拦截的请求
		if("/user/login.do".equals(request.getServletPath())){
			return true;
		}
		
		User user = (User)request.getSession().getAttribute("userSession");
		
		if(user == null){
			//没有登录
//			response.sendRedirect(request.getContextPath() + "/");
			throw new NoLoginException("用户未登录!");
//			return false;
		}
		return true;
	}

	/**
	 * 请求之后
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		System.out.println("LoginInterceptor postHandle ");
	}

}
